﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMTC.Model.Model;
using System.Data.Common;
using IMPlant.Core.Data.Unity;
using System.Data;

namespace SMTC.Dao.Role
{
   public class RoleProcessDao:BaseDao
    {
       /// <summary>
       /// 新建角色
       /// </summary>
       private const string SQL_INSERT_ROLE = @"INSERT INTO dbo.SMT_Role values(@RoleID,@RoleName,@Description)";
       /// <summary>
       /// 删除角色
       /// </summary>
       private const string SQL_DELETE_ROLE = @"DELETE dbo.SMT_Role WHERE RoleID = @RoleID";
       /// <summary>
       ///  添加角色成员
       /// </summary>
       private const string SQL_INSERT_MEMBER_IN_ROLE = @"INSERT INTO dbo.SMT_MemberInRole values(@RoleID,@MemberID)";
       /// <summary>
       /// 根据RoleID删除成员
       /// </summary>
       private const string SQL_DELETE_MEMBER_IN_ROLE = @"DELETE dbo.SMT_MemberInRole WHERE RoleID=@RoleID";
       /// <summary>
       /// 根据RoleID删除权限
       /// </summary>
       private const string SQL_DELETE_ROLEFUNCTION = @"DELETE dbo.SMT_RoleFunction WHERE RoleID=@RoleID";
       /// <summary>
       /// 更新角色权限
       /// </summary>
       private const string SQL_INSERT_ROLEFUNCTION = @"INSERT INTO dbo.SMT_RoleFunction VALUES(@FunctionID,@RoleID)";

       public bool InsertRole(SMT_RoleModel role, ref DbTransaction dbt)
       {
           DbTransaction localDbt = null;
           if (dbt == null)
           {
               dbt = DBTemplete.CreateDbTransaction();
               localDbt = dbt;
           }
           else
           {
               localDbt = dbt;
           }

           DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@RoleID",role.RoleID)
               ,DBparameterBuild.Build("@RoleName",role.RoleName)
               ,DBparameterBuild.Build("@Description",role.Description)
               
            };

           return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_INSERT_ROLE, parameters, dbt) > 0;
       }
       public bool DeleteRole(string RoleID, ref DbTransaction dbt)
       {
           DbTransaction localDbt = null;
           if (dbt == null)
           {
               dbt = DBTemplete.CreateDbTransaction();
               localDbt = dbt;
           }
           else
           {
               localDbt = dbt;
           }

           DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@RoleID",RoleID)
            };

           return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_DELETE_ROLE, parameters, dbt) > 0;
       }
       public bool DELETE_MEMBER_IN_ROLE(string RoleID, ref DbTransaction dbt)
       {
           DbTransaction localDbt = null;
           if (dbt == null)
           {
               dbt = DBTemplete.CreateDbTransaction();
               localDbt = dbt;
           }
           else
           {
               localDbt = dbt;
           }

           DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@RoleID",RoleID)
               
            };
           try
           {
               DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_DELETE_MEMBER_IN_ROLE, parameters, dbt);
               return true;
           }
           catch
           {
               return false;
           }
       }
       public bool INSERT_MEMBER_IN_ROLE(SMT_MemberInRole_Model model, ref DbTransaction dbt)
       {
           DbTransaction localDbt = null;
           if (dbt == null)
           {
               dbt = DBTemplete.CreateDbTransaction();
               localDbt = dbt;
           }
           else
           {
               localDbt = dbt;
           }

           DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@RoleID",model.RoleID)
               ,DBparameterBuild.Build("@MemberID",model.MemberID)
               
            };

           return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_INSERT_MEMBER_IN_ROLE, parameters, dbt) > 0;
       }
       public bool DELETE_ROLEFUNCTION(string RoleID, ref DbTransaction dbt)
       {
           DbTransaction localDbt = null;
           if (dbt == null)
           {
               dbt = DBTemplete.CreateDbTransaction();
               localDbt = dbt;
           }
           else
           {
               localDbt = dbt;
           }

           DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@RoleID",RoleID)
            };
           try
           {
               DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_DELETE_ROLEFUNCTION, parameters, dbt);
               return true;
           }
           catch
           {
               return false;
           }
       }
       public bool INSERT_ROLEFUNCTION(SMT_RoleFunctionModel R_F_Model, ref DbTransaction dbt)
       {
           DbTransaction localDbt = null;
           if (dbt == null)
           {
               dbt = DBTemplete.CreateDbTransaction();
               localDbt = dbt;
           }
           else
           {
               localDbt = dbt;
           }

           DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@FunctionID",R_F_Model.FunctionID)
               ,DBparameterBuild.Build("@RoleID",R_F_Model.RoleID)
            };

           return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_INSERT_ROLEFUNCTION, parameters, dbt) > 0;
       }
    }
}
